$(function () {
    $(".delete-link").click(function (event) {
        event.preventDefault();
        var url = $(this).attr('href');
        swal({
            title: '确定删除吗？',
            text: '你将无法恢复它！',
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: '确定删除！',
        }).then(function (e) {
            if (e.dismiss != 'cancel') {
                createDeleteForm(url)
            }
        })
    });

    function createDeleteForm(url) {
        var f = document.createElement('form');
        $(document.body).after($(f).attr({
            method: 'POST',
            action: url
        }).append('<input type="hidden" name="_method" value="delete" >'));
        $(f).submit();
    }

})



$(function () {
    $(".patch-link").click(function (event) {
        event.preventDefault();
        var url = $(this).attr('href');
        swal({
            title: '确定这样做吗?',
            showCancelButton: true,
            confirmButtonText: '确定！',
            cancelButtonText: '取消！'
        }).then(function (e) {
            if (e.dismiss === 'cancel') {
                swal(
                    '已取消！'
                )
            } else {
                createPatchForm(url);
                swal({
                    type: 'success',
                    title: '操作完成！'
                });
            }
        })
    });

    function createPatchForm(url) {
        var f = document.createElement('form');
        $(document.body).after($(f).attr({
            method: 'POST',
            action: url,
        }).append('<input type="hidden" name="_method" value="patch" >'));
        $(f).submit();
    }

})


$(function () {
    $(".get-link").click(function (event) {
        event.preventDefault();
        var url = $(this).attr('href');
        swal({
            title: '确定这样做吗?',
            showCancelButton: true,
            confirmButtonText: '确定！',
            cancelButtonText: '取消！'
        }).then(function (e) {
            if (e.dismiss === 'cancel') {
                swal(
                    '已取消！'
                )
            } else {
                createPatchForm(url);
                swal({
                    type: 'success',
                    title: '操作完成！'
                });
            }
        })
    });

    function createPatchForm(url) {
        var f = document.createElement('form');
        $(document.body).after($(f).attr({
            method: 'GET',
            action: url,
        }).append('<input type="hidden" name="_method" value="get" >'));
        $(f).submit();
    }

})


$(function () {
    $(document).on('click', ".refuse-link", function(event){
        event.preventDefault();
        var url = $(this).attr('href');
        swal({
            title: '请输入粉丝数量(整数)',
            input: 'text',
            showCancelButton: true,
            confirmButtonText: '提交',
            cancelButtonText: '取消！',
            showLoaderOnConfirm: true,
            preConfirm: function(e) {
                return new Promise(function(resolve, reject) {
                    setTimeout(function() {
                        if (e === '') {
                            reject();
                        } else {
                            resolve();
                        }
                    }, 500);
                }).catch(function(){
                    swal.showValidationMessage(
                        '粉丝数量不能为空'
                    )}
                )
            },
            allowOutsideClick: false
        }).then(function (e) {
            if (e.dismiss === 'cancel') {
                swal(
                    '已取消！'
                )
            } else {
                createRefuseForm(url, e.value);
                swal({
                    type: 'success',
                    title: '操作完成！'
                });
            }
        })
    });

    function createRefuseForm(url, refuse) {
        var f = document.createElement('form');
        $(document.body).after($(f).attr({
            method: 'POST',
            action: url + "?_method=patch"
        }).append('<input type="hidden" name="fansCount" value=' + refuse + ' >'));
        $(f).submit();
    }

})


$(function () {
    $(document).on('click', ".update-user-link", function(event){
        event.preventDefault();
        var url = $(this).attr('href');
        swal({
            title: '请输入修改后的积分',
            input: 'text',
            showCancelButton: true,
            confirmButtonText: '提交',
            cancelButtonText: '取消！',
            showLoaderOnConfirm: true,
            preConfirm: function(e) {
                return new Promise(function(resolve, reject) {
                    setTimeout(function() {
                        if (e === '') {
                            reject();
                        } else {
                            resolve();
                        }
                    }, 500);
                }).catch(function(){
                    swal.showValidationMessage(
                        '积分不能为空'
                    )}
                )
            },
            allowOutsideClick: false
        }).then(function (e) {
            if (e.dismiss === 'cancel') {
                swal(
                    '已取消！'
                )
            } else {
                createRefuseForm(url, e.value);
                swal({
                    type: 'success',
                    title: '操作完成！'
                });
            }
        })
    });

    function createRefuseForm(url, refuse) {
        var f = document.createElement('form');
        $(document.body).after($(f).attr({
            method: 'POST',
            action: url + "?_method=patch"
        }).append('<input type="hidden" name="userIntegral" value=' + refuse + ' >'));
        $(f).submit();
    }
})
